using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EM.utility;
using System.Data.SqlClient;

namespace EM.Common
{
    public partial class showCcmm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication Rua = new Authentication();
            Rua.CheckRolePageAuthentication(262, Authentication.RedirectionURLTypes.TimeOutPage);

            GetData();

        }

        private void GetData()
        {
            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();
            DataTable dt = new DataTable();
            int tid, pkc = 0;
            DateTime CREDATE, MODDATE;

            if (!Int32.TryParse(Request.QueryString["tid"].ToString(), out tid) || !Int32.TryParse(Request.QueryString["pkc"].ToString(), out pkc))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "CloseWindow", "window.close();", true);
            }

            string sSQL = "";

            sSQL += " SELECT TABLENAME , PK1 , PK2 FROM TBLTABLE WHERE TABLEID=@TABLEID ";
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("TABLEID", SqlDbType.Int).Value = tid;

            dt = srv.CreateDataTable(cmd, "");

            if (dt.Rows.Count > 0)
            {
                cmd = new SqlCommand();

                sSQL = " SELECT B.* , U.USERCODE AS MODUSER  ";
                sSQL += " FROM ( ";
                sSQL += " 	SELECT U.USERCODE AS CREUSER , A.CREDATE , A.MODUSERID , A.MODDATE ";
                sSQL += " 	FROM " + dt.Rows[0]["TABLENAME"].ToString() + " A LEFT OUTER JOIN TBLUSER U ON A.CREUSERID = U.USERID ";
                sSQL += " 	WHERE A." + dt.Rows[0]["PK1"].ToString() + " = @PK1  ";
                if (pkc == 2 && Request.QueryString["pk2"] != null)
                {
                    sSQL += " AND A." + dt.Rows[0]["PK2"].ToString() + " = @PK2    ";
                    cmd.Parameters.Add("@PK2", SqlDbType.NVarChar).Value = Request.QueryString["pk2"].ToString();
                }
                sSQL += " )B LEFT OUTER JOIN TBLUSER U ON B.MODUSERID = U.USERID ";

                cmd.CommandText = sSQL;
                try
                {
                    cmd.Parameters.Add("@PK1", SqlDbType.NVarChar).Value = Request.QueryString["pk1"].ToString();

                    dt = srv.CreateDataTable(cmd, "");

                    if (dt.Rows.Count > 0)
                    {
                        txtCreUser.Text = dt.Rows[0]["CREUSER"].ToString();
                        txtModUser.Text = dt.Rows[0]["MODUSER"].ToString();
                        if (DateTime.TryParse(dt.Rows[0]["CREDATE"].ToString(), out CREDATE))
                        {
                            txtCreDate.Text = CREDATE.ToString();
                        }

                        if (DateTime.TryParse(dt.Rows[0]["MODDATE"].ToString(), out MODDATE))
                        {
                            txtModDate.Text = MODDATE.ToString();
                        }

                    }
                }
                catch (Exception e)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('" + e.Message + "');", true);
                }
            }
        }
    }
}
